package com.ftg.learn.springbootmq.work.config;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ配置类
 *
 * @author 布衣
 * @since 2025-08-08
 */
@Configuration
public class RabbitMQConfig {

    /**
     * 定义工作队列
     *
     * @return Queue对象
     */
    @Bean
    public Queue workQueue() {
        // durable: 是否持久化
        // exclusive: 是否排他
        // autoDelete: 是否自动删除
        return new Queue("work.queue", true, false, false);
    }

    /**
     * 配置RabbitTemplate消息转换器
     *
     * @param connectionFactory 连接工厂
     * @return RabbitTemplate对象
     */
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        return rabbitTemplate;
    }
}

